package com.tan00xu.service.impl;

import com.baomidou.mybatisplus.core.assist.ISqlRunner;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.tan00xu.dao.ArticleTagMapper;
import com.tan00xu.dao.TagMapper;
import com.tan00xu.pojo.Article;
import com.tan00xu.pojo.ArticleTag;
import com.tan00xu.pojo.Tag;
import com.tan00xu.service.TagService;
import com.tan00xu.utils.CmdOutputInformationUtils;
import com.tan00xu.vo.Result;
import com.tan00xu.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private ArticleTagMapper articleTagMapper;

    @Override
    public List<TagVo> getTageByArticleId(Long articleId) {
        List<Tag> tags = tagMapper.getTageByArticleId(articleId);
        return copyList(tags);
    }


    @Override
    public Result listHotTags(int limit) {
        /*
          标签下的文章数量最多
          计数，从大到小排序 取前limit个
         */
        List<Long> tagIds = tagMapper.listHotTagIds(limit);

        if(CollectionUtils.isEmpty(tagIds)){
            //若是空的，返回空的数组
            return Result.success(Collections.emptyList());
        }
        //获取到热门标签列表
        List<Tag> tagList= tagMapper.listHotTagsByTagIds(tagIds);

        return Result.success(copyList(tagList));
    }

    /**
     * 获取所有的文章标签
     * @return Result
     */
    @Override
    public Result listAllTags() {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Tag::getId,Tag::getTagName);
        List<Tag> tagList = this.tagMapper.selectList(queryWrapper);

        return Result.success("获取所有的文章标签成功",copyList(tagList));
    }

    @Override
    public Result listAllTagsDetail() {
        List<Tag> tagList = this.tagMapper.selectList(null);

        return Result.success("获取所有的文章标签成功",copyList(tagList));
    }

    /**
     * 根据标签id获取标签详细信息
     * @param tagId 标签id
     * @return
     */
    @Override
    public Result getTagDetailByTagId(Long tagId) {
        Tag tag = this.tagMapper.selectById(tagId);
        return Result.success("获取该标签的详细信息成功",copy(tag));
    }

    /**
     * 根据标签id统计该标签下的文章数
     * @param tagId 标签id
     * @return Long文章数
     */
    @Override
    public Long countArticleByTagId(Long tagId) {
        LambdaQueryWrapper<ArticleTag> countWrapper = new LambdaQueryWrapper<>();
        countWrapper.eq(ArticleTag::getTagId, tagId);
        return this.articleTagMapper.selectCount(countWrapper);
    }

    public TagVo copy(Tag tag){
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(tag,tagVo);
        Long countArticle = countArticleByTagId(tag.getId());
        tagVo.setCountArticle(countArticle);
        return tagVo;
    }
    public List<TagVo> copyList(List<Tag> tagList){
        List<TagVo> tagVoList = new ArrayList<>();
        for (Tag tag : tagList) {
            tagVoList.add(copy(tag));
        }
        return tagVoList;
    }
}
